<?php

use Illuminate\Database\Seeder;
use App\MenuPermission;
use App\Route;
use App\Goods;
use App\Services\MenuPermissionService;
use App\Services\RouteService;

class GoodsSeeder extends Seeder
{
    private $menuPermissionService = null;
    private $routeService = null;
    /**
     * Run the database seeds.
     *
     * @return void
     */
    public function run()
    {
        $this->menuPermissionService = new MenuPermissionService();
        $this->routeService = new RouteService();

        $baseModule = $this->menuPermissionService->createMenuPermission([
            'name' => '商品管理',
            'permission_type' => MenuPermission::$PERMISSION_TYPE_TOP_NAVI,
            'parent_id' => null,
            'sort_order' => 0,
        ]);
        $this->createSystemManage($baseModule);
    }

    private function createSystemManage($baseModule)
    {
        $goods = $this->menuPermissionService->createMenuPermission([
            'name' => '商品管理',
            'icon' => '&#xe614;',
            'permission_type' => MenuPermission::$PERMISSION_TYPE_MENU_GROUP,
            'parent_id' => $baseModule->id,
            'sort_order' => 1,
        ]);

        $goodsManager = $this->menuPermissionService->createMenuPermission([
            'name' => '商品管理',
            'permission_type' => MenuPermission::$PERMISSION_TYPE_MENU,
            'route_id' => $this->routeService->createRoute([
                'name' => '商品管理主页面',
                'code' => 'perm_goods_manage',
                'route' => '/backstage/goods',
                'action' => 'Backstage\System\GoodsController@index',
                'slug' => 'goods.manage',
                'route_type' => Route::$ROUTE_TYPE_WEB,
                'method' => Route::$ROUTE_METHOD_GET,
                'sort_order' => 4,
            ])->id,
            'parent_id' => $goods->id,
            'sort_order' => 4,
        ]);

        $goodsQuery = $this->menuPermissionService->createMenuPermission([
            'name' => '查询商品',
            'permission_type' => MenuPermission::$PERMISSION_TYPE_OPERATION,
            'route_id' => $this->routeService->createRoute([
                'name' => '商品查询路由',
                'code' => 'perm_goods_query',
                'route' => '/backstage/api/goods/query',
                'action' => 'Backstage\System\GoodsController@query',
                'slug' => 'goods.api.query',
                'route_type' => Route::$ROUTE_TYPE_WEB,
                'method' => Route::$ROUTE_METHOD_GET,
                'sort_order' => 7,
            ])->id,
            'parent_id' => $goodsManager->id,
            'sort_order' => 1,
        ]);

        $goodCreate = $this->menuPermissionService->createMenuPermission([
            'name' => '新增商品',
            'permission_type' => MenuPermission::$PERMISSION_TYPE_OPERATION,
            'route_id' => $this->routeService->createRoute([
                'name' => '新增商品路由',
                'code' => 'perm_goods_create_new',
                'route' => '/backstage/goods/create',
                'action' => 'Backstage\System\GoodsController@create',
                'slug' => 'goods.manage.createnew',
                'route_type' => Route::$ROUTE_TYPE_WEB,
                'method' => Route::$ROUTE_METHOD_GET,
                'sort_order' => 8,
            ])->id,
            'parent_id' => $goodsManager->id,
            'sort_order' => 2,
        ]);

        $goodsCreateSave = $this->menuPermissionService->createMenuPermission([
            'name' => '保存新增',
            'permission_type' => MenuPermission::$PERMISSION_TYPE_OPERATION,
            'route_id' => $this->routeService->createRoute([
                'name' => '新增角色保存路由',
                'code' => 'perm_goods_save_new',
                'route' => '/backstage/api/goods/savenew',
                'action' => 'Backstage\System\GoodsController@saveNew',
                'slug' => 'goods.manage.savenew',
                'route_type' => Route::$ROUTE_TYPE_WEB,
                'method' => Route::$ROUTE_METHOD_POST,
                'sort_order' => 9,
            ])->id,
            'parent_id' => $goodsManager->id,
            'sort_order' => 3,
        ]);

        $goodsDelete = $this->menuPermissionService->createMenuPermission([
            'name' => '删除商品',
            'permission_type' => MenuPermission::$PERMISSION_TYPE_OPERATION,
            'route_id' => $this->routeService->createRoute([
                'name' => '删除商品路由',
                'code' => 'perm_goods_delete',
                'route' => '/backstage/api/goods/delete',
                'action' => 'Backstage\System\GoodsController@delete',
                'slug' => 'goods.manage.delete',
                'route_type' => Route::$ROUTE_TYPE_WEB,
                'method' => Route::$ROUTE_METHOD_POST,
                'sort_order' => 12,
            ])->id,
            'parent_id' => $goodsManager->id,
            'sort_order' => 6,
        ]);

        $goodsUpdate = $this->menuPermissionService->createMenuPermission([
            'name' => '进入修改商品页面',
            'permission_type' => MenuPermission::$PERMISSION_TYPE_OPERATION,
            'route_id' => $this->routeService->createRoute([
                'name' => '角色修改页面路由',
                'code' => 'perm_goods_edit',
                'route' => '/backstage/goods/edit/{commodityId}',
                'action' => 'Backstage\System\GoodsController@edit',
                'slug' => 'goods.manage.edit',
                'route_type' => Route::$ROUTE_TYPE_WEB,
                'method' => Route::$ROUTE_METHOD_GET,
                'sort_order' => 10,
            ])->id,
            'parent_id' => $goodsManager->id,
            'sort_order' => 4,
        ]);

        $goodsUpdateSave = $this->menuPermissionService->createMenuPermission([
            'name' => '保存修改',
            'permission_type' => MenuPermission::$PERMISSION_TYPE_OPERATION,
            'route_id' => $this->routeService->createRoute([
                'name' => '商品修改保存',
                'code' => 'perm_goods_save_update',
                'route' => '/backstage/api/goods/update',
                'action' => 'Backstage\System\GoodsController@update',
                'slug' => 'goods.manage.saveupdate',
                'route_type' => Route::$ROUTE_TYPE_WEB,
                'method' => Route::$ROUTE_METHOD_POST,
                'sort_order' => 11,
            ])->id,
            'parent_id' => $goodsManager->id,
            'sort_order' => 5,
        ]);
    }
}